package com.example.springbootschedule.cron;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

import static java.lang.Thread.sleep;

@Service
public class CronService {
    SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
    @Scheduled(fixedRateString = "${custon.scheduled.fixedRate}")
    public void reportCurrentTime(){
        System.out.println("每隔5秒执行一次"+dataFormat.format(new Date()));
        try{
            sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
//    @Scheduled(fixedRateString = "${custon.scheduled.fixedRate}")
//    public void reportCurrentTime(){
//        System.out.println("每隔5秒执行一次" );
//    }
//    @Scheduled(cron = "${custom.scheduled.cron}")
//    public void fixTimeExecution() {
//        System.out.println("当前时间 :"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//    }
//    @Scheduled(fixedDelayString = "${custom.scheduled.fixedDelay}")
//    public void runWithFixedDelay(){
//        System.out.println("上一次任务执行完成之后10秒后在执行 "+ dataFormat.format(new Date())+"执行");
//        try {
//            sleep(1000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//    }
}
